<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.3"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>FabGL: VGA/DirectVGA/DirectVGA.ino</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="doxygen-awesome.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">FabGL
   </div>
   <div id="projectbrief">ESP32 Display Controller and Graphics Library</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.3 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('_v_g_a_2_direct_v_g_a_2_direct_v_g_a_8ino-example.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle"><div class="title">VGA/DirectVGA/DirectVGA.ino</div></div>
</div><!--header-->
<div class="contents">
<p >Sample usage of VGADirectController base class for direct VGA control</p>
<div class="fragment"><div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment">  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - &lt;http://www.fabgl.com&gt;</span></div>
<div class="line"><span class="comment">  Copyright (c) 2019-2022 Fabrizio Di Vittorio.</span></div>
<div class="line"><span class="comment">  All rights reserved.</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">* Please contact fdivitto2013@gmail.com if you need a commercial license.</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">* This library and related software is available under GPL v3.</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">  FabGL is free software: you can redistribute it and/or modify</span></div>
<div class="line"><span class="comment">  it under the terms of the GNU General Public License as published by</span></div>
<div class="line"><span class="comment">  the Free Software Foundation, either version 3 of the License, or</span></div>
<div class="line"><span class="comment">  (at your option) any later version.</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">  FabGL is distributed in the hope that it will be useful,</span></div>
<div class="line"><span class="comment">  but WITHOUT ANY WARRANTY; without even the implied warranty of</span></div>
<div class="line"><span class="comment">  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></div>
<div class="line"><span class="comment">  GNU General Public License for more details.</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">  You should have received a copy of the GNU General Public License</span></div>
<div class="line"><span class="comment">  along with FabGL.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="fabgl_8h.html">fabgl.h</a>&quot;</span></div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><a id="_a0" name="_a0"></a><a class="code hl_class" href="classfabgl_1_1_v_g_a_direct_controller.html">fabgl::VGADirectController</a> DisplayController;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keyword">volatile</span> <span class="keywordtype">double</span>      objX                 = 300;</div>
<div class="line"><span class="keyword">volatile</span> <span class="keywordtype">double</span>      objY                 = 200;</div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">double</span>        objDir               = 0.3;</div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">double</span>        objVel               =  10;</div>
<div class="line"><span class="keyword">static</span> <span class="keyword">constexpr</span> <span class="keywordtype">int</span> objSize              =  25;</div>
<div class="line"><span class="keyword">static</span> uint32_t      bgParkMillerState    = 1;</div>
<div class="line"><span class="keyword">static</span> <span class="keyword">constexpr</span> <span class="keywordtype">int</span> borderSize           = 20;</div>
<div class="line"><span class="keyword">constexpr</span> <span class="keywordtype">int</span>        scanlinesPerCallback = 2;  <span class="comment">// screen height should be divisible by this value</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">static</span> TaskHandle_t  mainTaskHandle;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="comment">// just to avoid floating point calculations inside MyDirectDrawVGAController::drawScanline()</span></div>
<div class="line"><span class="keyword">volatile</span> <span class="keywordtype">int</span> objIntX;</div>
<div class="line"><span class="keyword">volatile</span> <span class="keywordtype">int</span> objIntY;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keyword">inline</span> <span class="keywordtype">int</span> fastRandom()</div>
<div class="line">{</div>
<div class="line">  bgParkMillerState = (uint64_t)bgParkMillerState * 48271 % 0x7fffffff;</div>
<div class="line">  <span class="keywordflow">return</span> bgParkMillerState % 4;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> IRAM_ATTR drawScanline(<span class="keywordtype">void</span> * arg, uint8_t * dest, <span class="keywordtype">int</span> scanLine)</div>
<div class="line">{</div>
<div class="line">  <span class="keyword">auto</span> fgcolor = DisplayController.createRawPixel(RGB222(3, 0, 0)); <span class="comment">// red</span></div>
<div class="line">  <span class="keyword">auto</span> bgcolor = DisplayController.createRawPixel(RGB222(0, 0, 2)); <span class="comment">// blue</span></div>
<div class="line"> </div>
<div class="line">  <span class="keyword">auto</span> <a id="a1" name="a1"></a><a class="code hl_variable" href="displaycontroller_8h_a2474a5474cbff19523a51eb1de01cda4.html#a2474a5474cbff19523a51eb1de01cda4">width</a>  = DisplayController.<a id="a2" name="a2"></a><a class="code hl_function" href="classfabgl_1_1_base_display_controller_a749df909b39fd096bf6e2813f2f5324c.html#a749df909b39fd096bf6e2813f2f5324c">getScreenWidth</a>();</div>
<div class="line">  <span class="keyword">auto</span> <a id="a3" name="a3"></a><a class="code hl_variable" href="displaycontroller_8h_ad12fc34ce789bce6c8a05d8a17138534.html#ad12fc34ce789bce6c8a05d8a17138534">height</a> = DisplayController.<a id="a4" name="a4"></a><a class="code hl_function" href="classfabgl_1_1_base_display_controller_a0872427e4a351bb3590e8d00096e9cba.html#a0872427e4a351bb3590e8d00096e9cba">getScreenHeight</a>();</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// draws &quot;scanlinesPerCallback&quot; scanlines every time drawScanline() is called</span></div>
<div class="line">  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; scanlinesPerCallback; ++i) {</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// fill upper and lower border with random background color</span></div>
<div class="line">    <span class="keywordflow">if</span> (scanLine &lt; borderSize || scanLine &gt; <a class="code hl_variable" href="displaycontroller_8h_ad12fc34ce789bce6c8a05d8a17138534.html#ad12fc34ce789bce6c8a05d8a17138534">height</a> - borderSize)</div>
<div class="line">      bgcolor = DisplayController.createRawPixel(RGB222(fastRandom(), fastRandom(), fastRandom()));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// fill line with background color</span></div>
<div class="line">    memset(dest, bgcolor, <a class="code hl_variable" href="displaycontroller_8h_a2474a5474cbff19523a51eb1de01cda4.html#a2474a5474cbff19523a51eb1de01cda4">width</a>);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// fill object with foreground color</span></div>
<div class="line">    <span class="keywordflow">if</span> (scanLine &gt;= objIntY - objSize / 2 &amp;&amp; scanLine &lt;= objIntY + objSize / 2) {</div>
<div class="line">      <span class="keywordflow">for</span> (<span class="keywordtype">int</span> col = objIntX - objSize / 2; col &lt; objIntX + objSize / 2; ++col) {</div>
<div class="line">        <span class="keywordflow">if</span> (col &gt;= 0 &amp;&amp; col &lt; <a class="code hl_variable" href="displaycontroller_8h_a2474a5474cbff19523a51eb1de01cda4.html#a2474a5474cbff19523a51eb1de01cda4">width</a>) {</div>
<div class="line">          VGA_PIXELINROW(dest, col) = fgcolor;</div>
<div class="line">        }</div>
<div class="line">      }</div>
<div class="line">    }</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// go to next scanline</span></div>
<div class="line">    ++scanLine;</div>
<div class="line">    dest += <a class="code hl_variable" href="displaycontroller_8h_a2474a5474cbff19523a51eb1de01cda4.html#a2474a5474cbff19523a51eb1de01cda4">width</a>;</div>
<div class="line"> </div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  <span class="keywordflow">if</span> (scanLine == <a class="code hl_variable" href="displaycontroller_8h_ad12fc34ce789bce6c8a05d8a17138534.html#ad12fc34ce789bce6c8a05d8a17138534">height</a>) {</div>
<div class="line">    <span class="comment">// signal end of screen</span></div>
<div class="line">    vTaskNotifyGiveFromISR(mainTaskHandle, NULL);</div>
<div class="line">  }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> setup()</div>
<div class="line">{</div>
<div class="line">  <span class="comment">//Serial.begin(115200); delay(500); Serial.write(&quot;\n\n\n&quot;); // DEBUG ONLY</span></div>
<div class="line"> </div>
<div class="line">  mainTaskHandle = xTaskGetCurrentTaskHandle();</div>
<div class="line"> </div>
<div class="line">  DisplayController.begin();</div>
<div class="line">  DisplayController.<a id="a5" name="a5"></a><a class="code hl_function" href="classfabgl_1_1_v_g_a_direct_controller_a22746803b0a00aea677779c4994b5980.html#a22746803b0a00aea677779c4994b5980">setScanlinesPerCallBack</a>(scanlinesPerCallback);</div>
<div class="line">  DisplayController.<a id="a6" name="a6"></a><a class="code hl_function" href="classfabgl_1_1_v_g_a_direct_controller_a18497a8f427eca28ee4bc4fed3a76508.html#a18497a8f427eca28ee4bc4fed3a76508">setDrawScanlineCallback</a>(drawScanline);</div>
<div class="line">  DisplayController.<a id="a7" name="a7"></a>setResolution(<a id="a8" name="a8"></a><a class="code hl_define" href="fabglconf_8h_a2d4571e9072b78877c36bf32bc9423f8.html#a2d4571e9072b78877c36bf32bc9423f8">VGA_640x480_60Hz</a>);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> loop()</div>
<div class="line">{</div>
<div class="line">  <span class="comment">// test collision with borders and bounce changing direction</span></div>
<div class="line">  <span class="keywordflow">if</span> (objX &lt; objSize / 2 || objX &gt; DisplayController.<a class="code hl_function" href="classfabgl_1_1_base_display_controller_a749df909b39fd096bf6e2813f2f5324c.html#a749df909b39fd096bf6e2813f2f5324c">getScreenWidth</a>() - objSize / 2)</div>
<div class="line">    objDir = PI - objDir;</div>
<div class="line">  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (objY &lt; borderSize + objSize / 2 || objY &gt; DisplayController.<a class="code hl_function" href="classfabgl_1_1_base_display_controller_a0872427e4a351bb3590e8d00096e9cba.html#a0872427e4a351bb3590e8d00096e9cba">getScreenHeight</a>() - borderSize - objSize / 2)</div>
<div class="line">    objDir = 2 * PI - objDir;</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// calculate new coordinates</span></div>
<div class="line">  objX += objVel * cos(objDir);</div>
<div class="line">  objY += objVel * sin(objDir);</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// convert object coordinate to integer</span></div>
<div class="line">  objIntX = objX;</div>
<div class="line">  objIntY = objY;</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// wait for vertical sync</span></div>
<div class="line">  ulTaskNotifyTake(pdTRUE, portMAX_DELAY);</div>
<div class="line">}</div>
<div class="ttc" id="aclassfabgl_1_1_base_display_controller_a0872427e4a351bb3590e8d00096e9cba_html_a0872427e4a351bb3590e8d00096e9cba"><div class="ttname"><a href="classfabgl_1_1_base_display_controller_a0872427e4a351bb3590e8d00096e9cba.html#a0872427e4a351bb3590e8d00096e9cba">fabgl::BaseDisplayController::getScreenHeight</a></div><div class="ttdeci">int getScreenHeight()</div><div class="ttdoc">Determines the screen height in pixels.</div><div class="ttdef"><b>Definition:</b> <a href="displaycontroller_8h_source.html#l00696">displaycontroller.h:696</a></div></div>
<div class="ttc" id="aclassfabgl_1_1_base_display_controller_a749df909b39fd096bf6e2813f2f5324c_html_a749df909b39fd096bf6e2813f2f5324c"><div class="ttname"><a href="classfabgl_1_1_base_display_controller_a749df909b39fd096bf6e2813f2f5324c.html#a749df909b39fd096bf6e2813f2f5324c">fabgl::BaseDisplayController::getScreenWidth</a></div><div class="ttdeci">int getScreenWidth()</div><div class="ttdoc">Determines the screen width in pixels.</div><div class="ttdef"><b>Definition:</b> <a href="displaycontroller_8h_source.html#l00689">displaycontroller.h:689</a></div></div>
<div class="ttc" id="aclassfabgl_1_1_v_g_a_direct_controller_a18497a8f427eca28ee4bc4fed3a76508_html_a18497a8f427eca28ee4bc4fed3a76508"><div class="ttname"><a href="classfabgl_1_1_v_g_a_direct_controller_a18497a8f427eca28ee4bc4fed3a76508.html#a18497a8f427eca28ee4bc4fed3a76508">fabgl::VGADirectController::setDrawScanlineCallback</a></div><div class="ttdeci">void setDrawScanlineCallback(DrawScanlineCallback drawScanlineCallback, void *arg=nullptr)</div><div class="ttdoc">Sets the callback used when VGADirectController needs to prepare a new scanline to be sent to the VGA...</div><div class="ttdef"><b>Definition:</b> <a href="vgadirectcontroller_8h_source.html#l00116">vgadirectcontroller.h:116</a></div></div>
<div class="ttc" id="aclassfabgl_1_1_v_g_a_direct_controller_a22746803b0a00aea677779c4994b5980_html_a22746803b0a00aea677779c4994b5980"><div class="ttname"><a href="classfabgl_1_1_v_g_a_direct_controller_a22746803b0a00aea677779c4994b5980.html#a22746803b0a00aea677779c4994b5980">fabgl::VGADirectController::setScanlinesPerCallBack</a></div><div class="ttdeci">void setScanlinesPerCallBack(int value)</div><div class="ttdoc">Sets number of scanlines to draw in a single callback.</div><div class="ttdef"><b>Definition:</b> <a href="vgadirectcontroller_8h_source.html#l00132">vgadirectcontroller.h:132</a></div></div>
<div class="ttc" id="aclassfabgl_1_1_v_g_a_direct_controller_html"><div class="ttname"><a href="classfabgl_1_1_v_g_a_direct_controller.html">fabgl::VGADirectController</a></div><div class="ttdoc">Represents a base abstract class for direct draw VGA controller.</div><div class="ttdef"><b>Definition:</b> <a href="vgadirectcontroller_8h_source.html#l00077">vgadirectcontroller.h:77</a></div></div>
<div class="ttc" id="adisplaycontroller_8h_a2474a5474cbff19523a51eb1de01cda4_html_a2474a5474cbff19523a51eb1de01cda4"><div class="ttname"><a href="displaycontroller_8h_a2474a5474cbff19523a51eb1de01cda4.html#a2474a5474cbff19523a51eb1de01cda4">width</a></div><div class="ttdeci">uint8_t width</div><div class="ttdef"><b>Definition:</b> <a href="displaycontroller_8h_source.html#l00002">displaycontroller.h:2</a></div></div>
<div class="ttc" id="adisplaycontroller_8h_ad12fc34ce789bce6c8a05d8a17138534_html_ad12fc34ce789bce6c8a05d8a17138534"><div class="ttname"><a href="displaycontroller_8h_ad12fc34ce789bce6c8a05d8a17138534.html#ad12fc34ce789bce6c8a05d8a17138534">height</a></div><div class="ttdeci">uint8_t height</div><div class="ttdef"><b>Definition:</b> <a href="displaycontroller_8h_source.html#l00003">displaycontroller.h:3</a></div></div>
<div class="ttc" id="afabgl_8h_html"><div class="ttname"><a href="fabgl_8h.html">fabgl.h</a></div><div class="ttdoc">This file is the all in one include file. Application can just include this file to use FabGL library...</div></div>
<div class="ttc" id="afabglconf_8h_a2d4571e9072b78877c36bf32bc9423f8_html_a2d4571e9072b78877c36bf32bc9423f8"><div class="ttname"><a href="fabglconf_8h_a2d4571e9072b78877c36bf32bc9423f8.html#a2d4571e9072b78877c36bf32bc9423f8">VGA_640x480_60Hz</a></div><div class="ttdeci">#define VGA_640x480_60Hz</div><div class="ttdef"><b>Definition:</b> <a href="fabglconf_8h_source.html#l00246">fabglconf.h:246</a></div></div>
</div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Mon Aug 29 2022 13:52:04 for FabGL by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.3 </li>
  </ul>
</div>
</body>
</html>
